package com.moon;

import java.util.Date;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.util.PreferencesUtils;
import com.util.Utils;
import com.widget.CircleImageView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MemberCenterActivity extends BaseActivity implements OnClickListener{
	JSONArray mChild;
	JSONArray mClasses;
	
	MyAdapter mAdapter;
	int mSelected = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_member_center);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.next).setOnClickListener(this);
		((TextView) findViewById(R.id.center)).setText("会员中心");
		
		final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) findViewById(R.id.listview);
		ptrlvHeadlineNews.setMode(com.handmark.pulltorefresh.library.PullToRefreshBase.Mode.DISABLED);
		ListView listview = ptrlvHeadlineNews.getRefreshableView();
		listview.setVerticalScrollBarEnabled(false);
		mAdapter = new MyAdapter();
		listview.setAdapter(mAdapter);
		
		listview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) 
			{
				if(arg2 < 1) return;
				mSelected = arg2 - 1;
				mAdapter.notifyDataSetChanged();
				
//				JSONObject item = mArrayList.getJSONObject(arg2 - 2);
//
//				String name = item.getString("username");
//				if(item.getString("alias") != null) name = item.getString("alias");
//				
//				Intent intent = new Intent(ClassmateActivity.this,PersonInfoActivity.class);
//				intent.putExtra("name", name);
//				intent.putExtra("userId",item.getString("id"));
//				startActivity(intent);
			}
		});
	}
	
	class MyAdapter extends BaseAdapter {
		public int getCount() {
			if(mChild == null) return 0;
			return mChild.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FoodHolder foodHold = null;

			if (convertView == null) {
				LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				foodHold = new FoodHolder();

				convertView = mInflater.inflate(R.layout.item_fee, null);
				
				foodHold.icon = (CircleImageView)convertView.findViewById(R.id.icon);
				foodHold.name = (TextView)convertView.findViewById(R.id.name);
				foodHold.content = (TextView)convertView.findViewById(R.id.content);
				foodHold.view = (View)convertView.findViewById(R.id.sep);
				
				convertView.setTag(foodHold);
			} else {
				foodHold = (FoodHolder) convertView.getTag();
			}
			
			JSONObject item = mChild.getJSONObject(position);
			String text = item.getString("name");
			
			int count = mClasses.size();
			for(int i =0; i < count; i++)
			{
				JSONObject classItem = mClasses.getJSONObject(i);
				if(classItem.getInteger("id") == item.getInteger("class_id"))
				{
					text += "(";
					text += classItem.getString("school_name");
					if(classItem.getString("class_alias") != null) text += classItem.getString("class_alias");
					else text += classItem.getString("class_name");
					text += ")";
					break;
				}
			}
			foodHold.name.setText(text);
			foodHold.content.setText("会员服务已到期，点击续费");
			
			if(item.getString("expire_date").length() > 10)
			{
				try
				{
					Date date = Utils.ConverToDate(item.getString("expire_date"), "yyyy-MM-dd HH:mm:ss");
					foodHold.content.setText("会员服务于" + Utils.ConverToString(date,"yyyy年MM月dd日") + "到期");
					
					Date now = new Date();

					foodHold.content.setTextColor(getResources().getColor(R.color.red));
					if(date.getYear() > now.getYear() || (date.getYear() == now.getYear() && date.getMonth() > now.getMonth()))
					{
						foodHold.content.setTextColor(getResources().getColor(R.color.blue));
					}
				}
				catch(Exception e){
					
				}
			}
			
			foodHold.view.setBackgroundColor(Color.WHITE);
			if(mSelected == position) foodHold.view.setBackgroundColor(getResources().getColor(R.color.green));
			return convertView;
		}

		public final class FoodHolder {
			public CircleImageView icon;
			public TextView name;
			public TextView content;
			public View view;
		}
	}
	
	@Override
	public void onClick(View arg0) {
		if(arg0.getId() == R.id.left) finish();
		else 
		{
			if(mSelected == -1)
			{
				this.showToast("您还未选择您的小孩");
				return;
			}
			
			JSONObject item = mChild.getJSONObject(mSelected);
			
			String text = item.getString("name");
			int count = mClasses.size();
			for(int i =0; i < count; i++)
			{
				JSONObject classItem = mClasses.getJSONObject(i);
				if(classItem.getInteger("id") == item.getInteger("class_id"))
				{
					text += "(";
					text += classItem.getString("school_name");
					if(classItem.getString("class_alias") != null) text += classItem.getString("class_alias");
					else text += classItem.getString("class_name");
					text += ")";
					break;
				}
			}
			
			Intent intent = new Intent(this,MemberDateActivity.class);
			intent.putExtra("childId", item.getString("student_id"));
			intent.putExtra("childName", text);
			intent.putExtra("childDate", "会员服务已到期");
			intent.putExtra("expire_date", item.getString("expire_date"));
			
			int flag = 0;
			if(item.getString("expire_date").length() > 10)
			{
				try
				{
					Date date = Utils.ConverToDate(item.getString("expire_date"), "yyyy-MM-dd HH:mm:ss");
					intent.putExtra("childDate", "会员服务于" + Utils.ConverToString(date,"yyyy年MM月dd日") + "到期");
					
					Date now = new Date();
					if(date.getYear() > now.getYear() || (date.getYear() == now.getYear() && date.getMonth() > now.getMonth()))
					{
						flag = 1;
					}
				}
				catch(Exception e){}
			}
			intent.putExtra("flag", flag);
			startActivity(intent);
		}
	}

	@Override
	protected void onResume()
	{
 		super.onResume();
		
		JSONObject member = JSON.parseObject(PreferencesUtils.getString(this, "member"));
		mChild = member.getJSONArray("children");
		mClasses = member.getJSONArray("classes");
		mAdapter.notifyDataSetChanged();
	}
}
